package components;

import java.lang.reflect.Type;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

import common.Constant;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import data.RCAlertSMSCommand;
import data.RCDevice;
import data.RCDeviceSMSCommand;
import data.RCUpdateSMSCommand;
import data.SMSCommand;
import data.SMSLog;

public class SMSLogManager {
	private static SMSLogManager slogManager = null;
	private ArrayList<SMSLog> mSMSLogList;
	private final static String SMS_LOG_LIST_KEY = "SMS_LOG_LIST_KEY";

	private SMSLogManager() {

	}

	public ArrayList<SMSLog> getSMSLogList() {
		return mSMSLogList;
	}
	public static SMSLogManager sharedSMSLogManager(Context context) {
		if (slogManager == null) {
			slogManager = new SMSLogManager();
			slogManager.loadSMSLogList(context);
		}

		return slogManager;
	}

	private void loadSMSLogList(Context context) {
		// TODO Auto-generated method stub
		SharedPreferences pref = context.getSharedPreferences(
				Constant.KEY_SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
		String s = pref.getString(SMS_LOG_LIST_KEY, null);

		if (s == null) {
			mSMSLogList = new ArrayList<SMSLog>();
		} else {
			try {
				SMSLog[] smsLogs = new Gson().fromJson(s, SMSLog[].class);
				ArrayList<SMSLog> result = new ArrayList<SMSLog>();
				result.addAll(Arrays.asList(smsLogs));
				mSMSLogList = result;

			} catch (JsonSyntaxException ex) {
				mSMSLogList = new ArrayList<SMSLog>();
			}

		}

	}

	/**
	 * 
	 * @param context
	 * @return
	 * @exception InvalidParameterException
	 */
	private void saveSMSLogList(Context context) {

		if (context == null) {
			throw new InvalidParameterException();
		}
		
		String json = new Gson().toJson(getSMSLogList());

		SharedPreferences pref = context.getSharedPreferences(
				Constant.KEY_SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
		Editor editor = pref.edit();

		editor.putString(SMS_LOG_LIST_KEY, json);
		editor.commit();
	}

	public void log(Context context, SMSCommand command) {
		if (command != null) {

			SMSLog log = new SMSLog();
			log.setSender(command.getPhoneNumber());
			log.setDate(new Date());
			log.setIsRead(false);
			String message = null;

			List<RCDevice> devices = AlarmManager.sharedAlarmList(context)
					.getDevices();

			if (command instanceof RCAlertSMSCommand) {
				message = "Nhận báo động của các thiết bị: ";

				for (int index : ((RCAlertSMSCommand) command)
						.getActiveDeviceIndexes()) {
					if (index < devices.size()) {
						String name = devices.get(index).getName();

						message = message + " [" + name + "]";
					}
				}
			} else if (command instanceof RCDeviceSMSCommand) {
				message = "Nhận trạng thái bật của các thiết bị: ";

				for (int index : ((RCDeviceSMSCommand) command)
						.getActiveDeviceIndexes()) {
					if (index < devices.size()) {
						String name = devices.get(index).getName();

						message = message + " [" + name + "]";
					}
				}
			} else if (command instanceof RCUpdateSMSCommand) {
				message = "Nhận cập nhật trạng thái của các thiết bị: ";

				
			}
			
			if (message != null) {
				log.setContent(message);
				this.add(context, log);
			}
		}
	}

	private void add(Context context, SMSLog smsLog) {

		if (smsLog == null) {
			return;
		}

		mSMSLogList.add(0, smsLog);
		saveSMSLogList(context);
	}

	public void clear(Context context) {

		mSMSLogList.clear();
		saveSMSLogList(context);
	}
}
